<template>
  <div class="page-content"><h2 id="5">获取用户信息和access_token</h2>
    <p>access_token是接口调用凭据，调用各接口时都需使用access_token。开发者需要进行妥善保存，access_token的有效期目前为24个小时，过期需要重新获取。</p>
    <p><strong>接口调用请求说明</strong></p>
    <p>http请求方式：POST/GET <a href="javascript:;">{{ addr }}/oauth/token/accesstoken?ticket=TICKET&amp;appid=APPID&amp;signature=SIGNATURE</a></p>
    <p><strong>请求示例</strong></p>
    <p><strong>参数说明</strong></p>
    <div class="table-ctn">
      <table>
        <thead>
          <tr>
            <th>参数</th>
            <th>说明</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>appid</td>
            <td>由“平台”分配给各实验课程的唯一编号</td>
          </tr>
          <tr>
            <td>signature</td>
            <td>
              <p>MD5(TICKET+ APPID+SECRET)</p>
              <p>注：</p><p>signature编码后需要改成全部大写字母</p>
              <p>TICKET点击“我要做实验”按钮获取的令牌，使用时需要做decode解码处理</p>
              <p>APPID是由“平台”分配给各实验平台的唯一编号（个人中心>实验管理>实验列表>APPID）</p>
              <p>SECRET是由“平台”分配给各实验平台的密钥（个人中心>实验管理>实验列表>SECRET）</p>
            </td>
          </tr>
          <tr>
            <td>ticket</td>
            <td>点击“我要做实验”按钮获取的ticket令牌，</td>
          </tr>
        </tbody>
      </table>
    </div>
    <p><strong>调用URL示例</strong></p>
    <p>{{ addr }}/oauth/token/accesstoken?<span style="color: red">ticket</span>=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJST05DT08iLCJleHAiOjE3MTM2OTIwNjUsInVzZXJJZCI6IjE3MDE4MDgxNjk3OTMxMzg2OTAifQ.FfUkZ_Z-Acwird66oHfg2PnWqhlY9g39i1Y9cOidgBI&amp;<span style="color: red">appid</span>=1681600222748241929&amp;<span style="color: red">signature</span>=6CC426EF80A31C5E7BDE10F6F5772DE5</p>
    <p><strong>返回结果</strong></p>
    <p>正确时的返回JSON数据包如下：</p>
    <pre><code>{
    <span class="key">"code":</span>  <span class="number">0</span>,
    <span class="key">"msg":</span>  <span class="string">"调用成功，返回所需数据"</span>,
    <span class="key">"access_token":</span>  <span class="string">"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtc2ciOiLosIPnlKjmiJDlip_vvIzov5Tlm57miYDpnIDmlbDmja4iLCJjb2RlIjowLCJjcmVhdGVfdGltZSI6MTcxMTEwMzIyOTEyMiwiY3JlYXRlX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjIgMTg6Mjc6MDkiLCJ1c2VyX25hbWUiOiIxNzAxODA4MTY5NzkzMTM4NjkwIiwiYXV0aG9yaXRpZXMiOlsiQURNSU4iXSwiY2xpZW50X2lkIjoiMTY4MTYwMDIyMjc0ODI0MTkyOSIsImRpcyI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJzY29wZSI6WyJjYWxsYmFjazpzY29yZSJdLCJleHBpcmVzX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjMgMTg6Mjc6MDgiLCJ1biI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJleHAiOjE3MTExODk2MjksImp0aSI6ImE1N2FiOTljLTZlZDMtNGI5My1hOGEzLTA3NzQ2NDM0ZmZlOCIsImV4cGlyZXNfdGltZSI6MTcxMTE4OTYyODEyMn0.rnRo5Z0D4R2p6jd57Ld29hAOegDtddKA4Z4sp4C7kD8"</span>,
    <span class="key">"token_type":</span>  <span class="string">"bearer"</span>,
    <span class="key">"refresh_token":</span>  <span class="string">"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtc2ciOiLosIPnlKjmiJDlip_vvIzov5Tlm57miYDpnIDmlbDmja4iLCJjb2RlIjowLCJjcmVhdGVfdGltZSI6MTcxMTEwMzIyOTEyMiwiY3JlYXRlX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjIgMTg6Mjc6MDkiLCJ1c2VyX25hbWUiOiIxNzAxODA4MTY5NzkzMTM4NjkwIiwiYXV0aG9yaXRpZXMiOlsiQURNSU4iXSwiY2xpZW50X2lkIjoiMTY4MTYwMDIyMjc0ODI0MTkyOSIsImRpcyI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJzY29wZSI6WyJjYWxsYmFjazpzY29yZSJdLCJhdGkiOiJhNTdhYjk5Yy02ZWQzLTRiOTMtYThhMy0wNzc0NjQzNGZmZTgiLCJleHBpcmVzX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjMgMTg6Mjc6MDgiLCJ1biI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJleHAiOjE3MTM2OTUyMjksImp0aSI6ImE3YTUwYzdmLTMxN2MtNGUzYy1iNTIwLWViZDhiMDI2ZmVjZiIsImV4cGlyZXNfdGltZSI6MTcxMTE4OTYyODEyMn0.CCIavimTmuyBcy6LxP9GHJHZSfurR9ILjhg-N2b9e48"</span>,
    <span class="key">"expires_in":</span>  <span class="number">86399</span>,
    <span class="key">"scope":</span>  <span class="string">"callback:score"</span>,
    <span class="key">"un":</span>  <span class="string">"1701808169793138690"</span>,
    <span class="key">"dis":</span>  <span class="string">"用户姓名"</span>,
    <span class="key">"create_time":</span>  <span class="string">"1711103229122"</span>,
    <span class="key">"create_time_display":</span>  <span class="string">"2024-03-22 18:27:09"</span>,
    <span class="key">"expires_time":</span>  <span class="string">"1711189628122"</span>,
    <span class="key">"expires_time_display":</span>  <span class="string">"2024-03-23 18:27:08"</span>,
    <span class="key">"jti":</span>  <span class="string">"a57ab99c-6ed3-4b93-a8a3-07746434ffe8"</span>
}</code></pre>
    <p><strong>返回数据说明：</strong></p>
    <div class="table-ctn">
      <table>
        <thead>
          <tr>
            <th>参数</th>
            <th>说明</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>access_token</td>
            <td>接口调用凭证</td>
          </tr>
          <tr>
            <td>create_time</td>
            <td>access_token创建时间，13位时间戳</td>
          </tr>
          <tr>
            <td>create_time_display</td>
            <td>示例：2020-08-24 08:54:21</td>
          </tr>
          <tr>
            <td>expires_time</td>
            <td>过期时间，13位时间戳</td>
          </tr>
          <tr>
            <td>expires_time_display</td>
            <td>示例：2020-08-24 10:54:21</td>
          </tr>
          <tr>
            <td>un</td>
            <td>用户名</td>
          </tr>
          <tr>
            <td>dis</td>
            <td>用户姓名</td>
          </tr>
        </tbody>
      </table>
    </div>
    <p>错误时的返回JSON数据包如下（示例为参数缺失）：</p>
    <pre><code>{
    <span class="key">"code":</span> <span class="number">1</span>,
    <span class="key">"msg":</span> <span class="string">"参数错误"</span>
}</code></pre>
    <p>
      <strong>返回码说明：</strong>
    </p>
    <div class="table-ctn">
      <table>
        <thead>
          <tr>
            <th>返回码</th>
            <th>说明</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>0</td>
            <td>调用成功，返回所需数据</td>
          </tr>
          <tr>
            <td>1</td>
            <td>参数错误：ticket缺失/appid缺失/signature缺失</td>
          </tr>
          <tr>
            <td>2</td>
            <td>密钥错误：signature不匹配。请注意签名时如有encode参数，均需要decode解码处理</td>
          </tr>
          <tr>
            <td>3</td>
            <td>ticket过期</td>
          </tr>
          <tr>
            <td>4</td>
            <td>无效ticket</td>
          </tr>
          <tr>
            <td>其他值</td>
            <td>系统错误或非法操作</td>
          </tr>
        </tbody>
      </table>
    </div>
    <p><strong>Python示例</strong></p>
    <pre><code>import json<br>from urllib.parse import quote, unquote<br>import urllib3<br>import hashlib<br><br>http = urllib3.PoolManager()<br><br>def get_access_token():<br>    APPID = '1681600222748241929'<br>    SECRET = '123456'<br>    API_PATH = '{{ addr }}'<br><br>    # 此处是从Url中获取到的Ticket，需要先进行解码<br>    TICKET = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJST05DT08iLCJleHAiOjE3MTM2OTIwNjUsInVzZXJJZCI6IjE3MDE4MDgxNjk3OTMxMzg2OTAifQ.FfUkZ_Z-Acwird66oHfg2PnWqhlY9g39i1Y9cOidgBI'<br>    text = unquote(TICKET) + APPID + SECRET<br>    hl = hashlib.md5()<br>    hl.update(text.encode(encoding='utf8'))<br>    signature = hl.hexdigest().upper()<br><br>    # 此处是在url中使用TICKET，需要使用url编码后的ticket<br>    path = API_PATH + '/oauth/token/accesstoken?ticket=' + TICKET + '&amp;appid=' + APPID + '&amp;signature=' + signature<br>    r = http.request("GET", path)<br>    return json.loads(r.data.decode('utf-8'))['access_token']<br><br><br>if __name__ == '__main__':<br>    ACCESS_TOKEN = get_access_token()<br>    print(ACCESS_TOKEN)<br></code></pre>
    <p><strong>Python示例运行结果：</strong></p>
    <p />
    <pre>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtc2ciOiLosIPnlKjmiJDlip_vvIzov5Tlm57miYDpnIDmlbDmja4iLCJjb2RlIjowLCJjcmVhdGVfdGltZSI6MTcxMTEwMzIyOTEyMiwiY3JlYXRlX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjIgMTg6Mjc6MDkiLCJ1c2VyX25hbWUiOiIxNzAxODA4MTY5NzkzMTM4NjkwIiwiYXV0aG9yaXRpZXMiOlsiQURNSU4iXSwiY2xpZW50X2lkIjoiMTY4MTYwMDIyMjc0ODI0MTkyOSIsImRpcyI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJzY29wZSI6WyJjYWxsYmFjazpzY29yZSJdLCJleHBpcmVzX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjMgMTg6Mjc6MDgiLCJ1biI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJleHAiOjE3MTExODk2MjksImp0aSI6ImE1N2FiOTljLTZlZDMtNGI5My1hOGEzLTA3NzQ2NDM0ZmZlOCIsImV4cGlyZXNfdGltZSI6MTcxMTE4OTYyODEyMn0.rnRo5Z0D4R2p6jd57Ld29hAOegDtddKA4Z4sp4C7kD8=</pre>
    <p />
    <p />
    <p><strong>特别说明</strong></p>
    <p>1.接口支持前端语言Javascript使用。<a href="javascript:;" @click="downloadApi">《jquery 调用示例》</a></p>
    <p>2.签名时如有encode参数，均需要decode解码处理。示例如下：</p>
    <p>access_token 原文示例</p>
    <p />
    <pre>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtc2ciOiLosIPnlKjmiJDlip_vvIzov5Tlm57miYDpnIDmlbDmja4iLCJjb2RlIjowLCJjcmVhdGVfdGltZSI6MTcxMTEwMzIyOTEyMiwiY3JlYXRlX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjIgMTg6Mjc6MDkiLCJ1c2VyX25hbWUiOiIxNzAxODA4MTY5NzkzMTM4NjkwIiwiYXV0aG9yaXRpZXMiOlsiQURNSU4iXSwiY2xpZW50X2lkIjoiMTY4MTYwMDIyMjc0ODI0MTkyOSIsImRpcyI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJzY29wZSI6WyJjYWxsYmFjazpzY29yZSJdLCJleHBpcmVzX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjMgMTg6Mjc6MDgiLCJ1biI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJleHAiOjE3MTExODk2MjksImp0aSI6ImE1N2FiOTljLTZlZDMtNGI5My1hOGEzLTA3NzQ2NDM0ZmZlOCIsImV4cGlyZXNfdGltZSI6MTcxMTE4OTYyODEyMn0.rnRo5Z0D4R2p6jd57Ld29hAOegDtddKA4Z4sp4C7kD8</pre>
    <p />
    <p>access_token url编码后示例</p>
    <p />
    <pre>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtc2ciOiLosIPnlKjmiJDlip_vvIzov5Tlm57miYDpnIDmlbDmja4iLCJjb2RlIjowLCJjcmVhdGVfdGltZSI6MTcxMTEwMzIyOTEyMiwiY3JlYXRlX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjIgMTg6Mjc6MDkiLCJ1c2VyX25hbWUiOiIxNzAxODA4MTY5NzkzMTM4NjkwIiwiYXV0aG9yaXRpZXMiOlsiQURNSU4iXSwiY2xpZW50X2lkIjoiMTY4MTYwMDIyMjc0ODI0MTkyOSIsImRpcyI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJzY29wZSI6WyJjYWxsYmFjazpzY29yZSJdLCJleHBpcmVzX3RpbWVfZGlzcGxheSI6IjIwMjQtMDMtMjMgMTg6Mjc6MDgiLCJ1biI6IjE3MDE4MDgxNjk3OTMxMzg2OTAiLCJleHAiOjE3MTExODk2MjksImp0aSI6ImE1N2FiOTljLTZlZDMtNGI5My1hOGEzLTA3NzQ2NDM0ZmZlOCIsImV4cGlyZXNfdGltZSI6MTcxMTE4OTYyODEyMn0.rnRo5Z0D4R2p6jd57Ld29hAOegDtddKA4Z4sp4C7kD8</pre>
    <p />
    <p>3. 编码使用原则</p>
    <p>进行MD5操作时需要使用原文，拼接URL的时候需要使用URL编码。</p>
  </div>
</template>
<script>
export default {
  data() {
    return {
      addr: ''
    }
  },
  mounted() {
    if (process.client) {
      this.addr = window.location.protocol + '//' + window.location.hostname + ':8180'
    }
  },
  methods: {
    downloadApi() {
      window.open(this.addr + '/jquery-api.html', '_blank')
      return false
    }
  }
}
</script>
